package com.yixun.qfbao.response.premises.detail;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.yixun.qfbao.xf.model.ApartmentLayoutDiagram;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zilong.liu
 * @date 2020-8-12
 * 	户型信息
 */
@Data
public class ApartmentInfo implements Serializable{
	
	public ApartmentInfo(){
		
	}

	public ApartmentInfo(ApartmentLayoutDiagram apartmentLayoutDiagram) {
		String urls = apartmentLayoutDiagram.getUrl();
		if(StringUtils.isNotBlank(urls)) {
			this.urlArray = urls.split(";");
		}
		this.apartmentLayout = apartmentLayoutDiagram.getApartmentLayout();
		String realAveragePrice = apartmentLayoutDiagram.getAveragePrice();
		this.averagePrice = convertInfo(realAveragePrice); 
		
		this.orientations = apartmentLayoutDiagram.getOrientations();
		this.area = convertInfo(apartmentLayoutDiagram.getArea());
		if(apartmentLayout != null) {
			String numberStr = apartmentLayout.substring(0, apartmentLayout.indexOf("室"));
			if("无卧".equals(numberStr)) {
				this.roomNumber = 0;
				apartmentLayout = apartmentLayout.replace("无卧", "0");
				apartmentLayout = apartmentLayout.replace("卫生间", "卫");
				int index = apartmentLayout.indexOf("卫");
				//去除阳台
				if (index > -1) {
					apartmentLayout = apartmentLayout.substring(0, index);
				}
			}
			try {
				this.roomNumber = Integer.parseInt(numberStr);
			} catch (Exception e) {
			}
		}
		
	}
	
	/**
	 * 转换 - 至， 并按照规则返回
	 * @param info
	 * @return
	 */
	private String convertInfo(String info) {
		if(StringUtils.isEmpty(info)) { 
			return "";
		}
		int index = info.indexOf(",");
		if(index < 0) {
			return info;
		}
		String[] infos = info.split(",", -1);
		if(infos[0].equals(infos[1])) {
			return infos[0];
		}
		if("".equals(infos[1])){
			return infos[0];
		}
		if("".equals(infos[0])){
			return infos[1];
		}
		return infos[0] + "-" + infos[1]; 
	}
	
	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "户型")
	private String apartmentLayout;
	
	@ApiModelProperty(value = "户型图片链接")
	private String[] urlArray;
	
	@ApiModelProperty(value = "户型均价")
	private String averagePrice;
	
	@ApiModelProperty(value = "朝向")
	private String orientations;
	
	@ApiModelProperty(value = "面积")
	private String area;
	
	@ApiModelProperty(value = "室的数量，比如一室为1")
	private int roomNumber;
}
